package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcBedEntity;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.entity.CdcRoomEntity;
import com.dhcc.cdc.busi.service.ICdcBedService;
import com.dhcc.cdc.busi.service.ICdcLocationService;
import com.dhcc.cdc.busi.service.ICdcRoomService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 床位
 * 
 * @ClassName: CdcBedController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:08
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcbed")
public class CdcBedController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcbed/";

	@Autowired
	private ICdcBedService cdcBedService;
    @Autowired
    private ICdcRoomService cdcRoomService;
    @Autowired
    private ICdcLocationService cdcLocationService;
	

    /**
     * 跳转到床位列表首页
     */
    @RequestMapping("")
    @Permission("cdcbed:list")
    public String index() {
        return PREFIX + "cdcbed.html";
    }
	
    /**
     * 跳转到新增床位
     */
    @RequestMapping("/to_add")
    @Permission("cdcbed:add")
    public String toAdd() {
        return PREFIX + "cdcbed_add.html";
    }
	
	/**
     * 跳转到修改床位
     */
    @RequestMapping("/to_update/{cdcBedId}")
    @Permission("cdcbed:update")
    public String toUpdate(@PathVariable Long cdcBedId, Model model) {
        CdcBedEntity cdcBed = this.cdcBedService.selectById(cdcBedId);
        if (CommonUtil.isNotEmpty(cdcBed.getRoomId())) {
            CdcRoomEntity room = this.cdcRoomService.selectById(cdcBed.getRoomId());
            cdcBed.setRoom(room);
            CdcLocationEntity location = this.cdcLocationService.selectById(room.getLocationId());
            cdcBed.setLocation(location);
            cdcBed.setHospId(location.getHospitalId());
        }
        model.addAttribute("cdcBed",cdcBed);
        return PREFIX + "cdcbed_edit.html";
    }

    /**
     * 获取床位列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcbed:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcBedEntity> page = new PageFactory<CdcBedEntity>().defaultPage();
        List<CdcBedEntity> result = this.cdcBedService.page(page,params);
        page.setRecords((List<CdcBedEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增床位
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcbed:add")
    @BussinessLog(value = "新增床位")
    public Object add(CdcBedEntity cdcBed) {
        if (CommonUtil.isOneEmpty(cdcBed)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcBed.insert();
        return R.ok();
    }

    /**
     * 删除床位
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcbed:delete")
    @BussinessLog(value = "删除床位")
    public Object delete(@RequestParam Long cdcBedId) {
	
        this.cdcBedService.deleteById(cdcBedId);

        return R.ok();
    }

    /**
     * 修改床位
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcbed:update")
    @BussinessLog(value = "修改床位")
    public Object update(CdcBedEntity cdcBed) {
        if (CommonUtil.isOneEmpty(cdcBed)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcBedEntity old = this.cdcBedService.selectById(cdcBed.getId());

        old.setXcode(cdcBed.getXcode());
        old.setCode(cdcBed.getCode());
        old.setDescription(cdcBed.getDescription());
        old.setRoomId(cdcBed.getRoomId());
        old.setDataId(cdcBed.getDataId());
        old.setIsActive(cdcBed.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
